package com.baizhi.service.impl;

import com.baizhi.entity.User;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.UserService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {

    @Override
    public User login(String username, String password) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        User user = null;
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            user = userMapper.selectUserByUsername(username);
            sqlSession.commit();
            if (user == null || !user.getPassword().equals(password)) {
                return null;
            }

        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return user;
    }
}
